package com.imdroid.lite.model;

import java.io.File;
import java.util.Calendar;

import android.media.MediaMetadataRetriever;

/**
 * The team talk
 * 
 */
public class TeamTalk {
	/**
	 * The team member
	 */
	private int memberId;
	/**
	 * The team member
	 */
	private String memberName;

	/**
	 * The file of the sound
	 */
	private String file;
	
	/**
	 * The file name in server
	 */
	private String fileNameInServer;
	
	/**
	 * The size of the file
	 */
	private int size;
	
	/**
	 * The length in seconds.
	 */
	private int length;
	
	/**
	 * Determine if the time is showed on the list.
	 */
	private boolean isTimeShowed;

	/**
	 * Get the id of the member
	 * 
	 * @return the memberId
	 */
	public int getMemberId() {
		return memberId;
	}

	/**
	 * Set the id of the member
	 * 
	 * @param memberId
	 *            the memberId to set
	 */
	public void setMemberId(int memberId) {
		this.memberId = memberId;
	}

	/**
	 * Set the name of the member
	 * 
	 * @return the memberName
	 */
	public String getMemberName() {
		return memberName;
	}

	/**
	 * Set the name of the member
	 * 
	 * @param memberName
	 *            the memberName to set
	 */
	public void setMemberName(String memberName) {
		this.memberName = memberName;
	}

	/**
	 * Get the name of the file
	 * 
	 * @return the file
	 */
	public String getFile() {
		return file;
	}

	/**
	 * Set the name fo the file
	 * 
	 * @param file
	 *            the file to set
	 */
	public void setFile(String file) {
		this.file = file;
		File newfile = new File(file);
		if(newfile.exists())
			calculateAndSetLength();
	}
	
	/**
	 * Calculate and set the length.
	 */
	public void calculateAndSetLength(){
		try{
			MediaMetadataRetriever mmr = new MediaMetadataRetriever();
			mmr.setDataSource(file);
			String length = mmr.extractMetadata(MediaMetadataRetriever.METADATA_KEY_DURATION);
			int len = Integer.parseInt(length);
			setLength((int)((len / 1000.0) + 0.5));
			mmr.release();
			System.out.println(length + "---" + file);
		} catch(Exception e){
			e.printStackTrace();
		}
	}

	/**
	 * Get the file size
	 * 
	 * @return the size
	 */
	public int getSize() {
		return size;
	}

	/**
	 * Set the file size
	 * 
	 * @param size
	 *            the size to set
	 */
	public void setSize(int size) {
		this.size = size;
	}

	/**
	 * @return the length
	 */
	public int getLength() {
		return length;
	}

	/**
	 * @param length the length to set
	 */
	public void setLength(int length) {
		this.length = length;
	}

	/**
	 * @return the fileNameInServer
	 */
	public String getFileNameInServer() {
		return fileNameInServer;
	}

	/**
	 * @param fileNameInServer the fileNameInServer to set
	 */
	public void setFileNameInServer(String fileNameInServer) {
		this.fileNameInServer = fileNameInServer;
	}

	/**
	 * @return the isTimeShowed
	 */
	public boolean isTimeShowed() {
		return isTimeShowed;
	}

	/**
	 * @param isTimeShowed the isTimeShowed to set
	 */
	public void setTimeShowed(boolean isTimeShowed) {
		this.isTimeShowed = isTimeShowed;
	}
	
	/**
	 * Get the sending time of this talk.
	 * @return
	 * 	"HH:MM" format time
	 */
	public String getTime(){
		Calendar c = Calendar.getInstance();
		c.setTimeInMillis(getRecordTime());
		return c.get(Calendar.HOUR_OF_DAY) + ":" + c.get(Calendar.MINUTE);
	}
	
	/**
	 * The time between the two talk.
	 * @param tt
	 * 	The given talk to compare with.
	 * @return
	 *  The time range in milliseconds, less than zero means the given talk is earlier than self.
	 */
	public long compareTime(TeamTalk tt){
		return tt.getRecordTime() - getRecordTime();
	}
	
	private long getRecordTime(){
		String src = getFile().substring(0, getFile().lastIndexOf('_'));
		src = src.substring(src.lastIndexOf('_') + 1);
		return Long.parseLong(src);
	}

}
